<?php

class hdms_userAdminController extends FoundryControllerInterface
{


	/**
	 * init()
	 *
	 * init() is called in place of the __construct() method used by the
	 * FoundryControllerInterface super class
	 * This function will be called immediately after the __construct method
	 *
	 */ 
	public function init()
	{
	
	
	}
	
	public function main()
	{
	
		return true;
	}
	
	public function edit()
	{
		$session = FoundryRegistry::get( 'USER_session');
		
		$id = FoundryRequest::get( 'id', 'num' );
		
		
		if( !$id || !is_numeric( $id ) ) {
			$id = false;
		} else {
			$user = $this->user( $id );
		}
		
		if( $session->checkKey() ) {
			$fullname = cleanString( FoundryRequest::post( 'userFullname', 'alnum' ) );
			$pass1 = cleanString( FoundryRequest::post( 'userPass1', 'alnum' ) );
			$pass2 = cleanString( FoundryRequest::post( 'userPass2', 'alnum' ) );
			$email = cleanString( FoundryRequest::post( 'userEmail', 'alnum' ) );
			$group_id = FoundryRequest::post( 'userGroupID', 'num' );
									
			if( $fullname && $group_id ) {
				
				if( $id === false ) {
					$user = $this->user();
					
					$username = cleanString( FoundryRequest::post( 'userUsername', 'alnum' ) );
					$user->username = $username;
				}
				
				$user->fullname = $fullname;
				$user->email = $email;
				$user->group_id = $group_id;
				
				if( strlen( $pass1 ) > 1 && $pass1 == $pass2 ) {
					$pass = md5( $pass1 );
					
					$user->password = $pass;
				}
				
				$user->save();
					
				header( "Location: ".
					FoundryView::helperLinkTo( 'userAdmin/' ) );
				exit;
					
				
			}
		}
		
		$key = $session->generateKey();
	
		return array( 'key' => $key, 'user'	=> $user );
	}
	
	public function deleteUser()
	{
		$id = FoundryRequest::post( 'id', 'num' );

		if( $id ) {
			$user = $this->user( $id );
			
			if( $user->uid ) {
				$user->delete();
				
				return $user;
			}
		}
		
		return $this->user();
	}
	
	public function editGroup()
	{
		$session = FoundryRegistry::get( 'USER_session');
		
		$id = FoundryRequest::get( 'id', 'num' );
		
		$temp = FoundryController::config( 'modules' );
		
		$allMods = array();
		foreach( $temp as $key => $mod ) {
			if( $mod['__obKey'] ) {
				$allMods[$key] = $mod;
			}
		}
		
		if( !$id || !is_numeric( $id ) ) {
			$id = false;
		} else {
			$group = $this->group( $id );
		}
		
		if( $session->checkKey() ) {
			$name = cleanString( FoundryRequest::post( 'groupName', 'alnum' ) );
			$role = FoundryRequest::post( 'groupRole', 'valid_array' );

			if( $name && !empty( $role ) ) {
				
				if( $id === false ) {
					$group = $this->group();
				}
				
				$group->name = $name;
				$group->role = $role;
				
				$group->save();
					
				header( "Location: ".
					FoundryView::helperLinkTo( 'userAdmin/' ) );
				exit;
					
				
			}
		}
		
		$key = $session->generateKey();
		$mask = FoundryRegistry::get( 'moduleMask' );
	
		return array( 'key' => $key, 'group' => $group, 'modules' => $allMods,
			'mask' => $mask );
	}
	
	public function deleteGroup()
	{
	
	}

}

?>